package com.baor.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.CustomerDao;
import com.baor.dao.DeliveryRecordDao;
import com.baor.domain.CustomerVO;
import com.baor.domain.DeliveryRecordVO;
import com.baor.service.DeliveryRankingService;
import com.baor.util.R;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeliveryRankingServiceImpl implements DeliveryRankingService {

    @Autowired
    private DeliveryRecordDao deliveryRecordDao;

    @Autowired
    private CustomerDao customerDao;


    @Override
    public R deliveryRecord(Map<String, Object> params) {
        int pageIndex = params.get("pageIndex") != null ? Integer.parseInt(params.get("pageIndex").toString()) : 1;
        int pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;
        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);

        MPJLambdaWrapper<DeliveryRecordVO> wrapper = new MPJLambdaWrapper<>(DeliveryRecordVO.class)
                .select(DeliveryRecordVO::getWeight)
                .select(CustomerVO::getNums,CustomerVO::getTotalweight)
                .eq(DeliveryRecordVO::getUserId,params.get("userId"))
                .like(DeliveryRecordVO::getDeliveryTime,params.get("month"))
                .leftJoin(CustomerVO.class,CustomerVO::getId,DeliveryRecordVO::getUserId);

        IPage<Map<String, Object>> resultPage = deliveryRecordDao.selectJoinMapsPage(page, wrapper);

        // 计算weight总和
        double MonthtotalWeight = resultPage.getRecords().stream()
                .mapToDouble(record -> {
                    Object weight = record.get("weight");
                    return weight != null ? Double.parseDouble(weight.toString()) : 0.0;
                })
                .sum();

        // 转换为前端需要的格式
        List<Map<String, Object>> resultList = resultPage.getRecords().stream()
                .map(this::convertToFrontendFormat)
                .collect(Collectors.toList());

        // 封装返回数据
        Map<String, Object> result = new HashMap<>();
        result.put("list", resultList);
        result.put("totalSize", resultPage.getTotal()); //当月投递次数
        result.put("pageIndex", pageIndex);
        result.put("pageSize", pageSize);
        result.put("MonthtotalWeight", MonthtotalWeight);  //当月重量

        return R.ok(result);
    }

    //
    private Map<String, Object> convertToFrontendFormat(Map<String, Object> record) {
        Map<String, Object> item = new HashMap<>();
        // 关联表字段
        item.put("nums", record.get("nums"));
//        item.put("weight", record.get("weight"));
        item.put("totalweight", record.get("totalweight"));
        return item;
    }

    /**
     *投递次数排行
     */
    @Override
    public R MyRankingNums(Map<String, Object> params) {

        int pageIndex = params.get("pageIndex") != null ? Integer.parseInt(params.get("pageIndex").toString()) : 1;
        int pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;
        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);
        MPJLambdaWrapper<CustomerVO> wrapper = new MPJLambdaWrapper<>(CustomerVO.class)
                .select(CustomerVO::getNums,CustomerVO::getNickname)
                .orderByDesc(CustomerVO::getNums);

        IPage<Map<String, Object>> resultPage = customerDao.selectJoinMapsPage(page, wrapper);
        List<Map<String, Object>> resultList = resultPage.getRecords().stream()
                .map(this::convertToFrontendFormat2)
                .collect(Collectors.toList());

        Map<String, Object> result = new HashMap<>();
        result.put("list", resultList);
        result.put("pageIndex", pageIndex);
        result.put("pageSize", pageSize);
        return R.ok(result);

    }

    private Map<String, Object> convertToFrontendFormat2(Map<String, Object> record) {
        Map<String, Object> item = new HashMap<>();
        // 关联表字段
        item.put("nums", record.get("nums"));
//        item.put("totalweight", record.get("totalweight"));
        item.put("nickname", record.get("nickname"));
        return item;
    }

    /**
     *投递重量排行
     */
    @Override
    public R MyRankingWeight(Map<String, Object> params) {
        int pageIndex = params.get("pageIndex") != null ? Integer.parseInt(params.get("pageIndex").toString()) : 1;
        int pageSize = params.get("pageSize") != null ? Integer.parseInt(params.get("pageSize").toString()) : 10;
        Page<Map<String, Object>> page = new Page<>(pageIndex, pageSize);

        MPJLambdaWrapper<CustomerVO> wrapper = new MPJLambdaWrapper<>(CustomerVO.class)
                .select(CustomerVO::getTotalweight,CustomerVO::getNickname)
                .orderByDesc(CustomerVO::getNums);

        IPage<Map<String, Object>> resultPage = customerDao.selectJoinMapsPage(page, wrapper);
        List<Map<String, Object>> resultList = resultPage.getRecords().stream()
                .map(this::convertToFrontendFormat3)
                .collect(Collectors.toList());


        Map<String, Object> result = new HashMap<>();
        result.put("list", resultList);
        result.put("pageIndex", pageIndex);
        result.put("pageSize", pageSize);
        return R.ok(result);

    }

    private Map<String, Object> convertToFrontendFormat3(Map<String, Object> record) {
        Map<String, Object> item = new HashMap<>();
        item.put("totalweight", record.get("totalweight"));
        item.put("nickname", record.get("nickname"));
        return item;
    }
}
